package com.atguigu.eduservice.controller.front;


import com.aiguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-09-21
 */
//@Slf4j
@Api(value = "讲师前台展示管理")
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/admin/teacher")
public class AdminEduTeacherController {
    /**
     * 讲师服务
     */
    @Resource
    private EduTeacherService teacherService;
    /**
     * 讲师服务
     */
    @Resource
    private EduCourseService eduCourseService;

    /**
     * 日志服务
     */
//    private static Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 1 查询讲师表的所有数据
     *
     * @return
     */
    @ApiOperation("查询讲师表的热点数据 4条数据")
    @GetMapping("/listEntity")
    public R listEntity() {
        List<EduTeacher> list = teacherService.listByLimit4();
        return R.ok().data("info", list);
    }


    @PostMapping("/getTeacherFrontList/{page}/{limit}")
    public R getTeacherFrontList(@ApiParam(name = "page", value = "当前页码", required = true)
                                 @PathVariable
                                         Long page,
                                 @ApiParam(name = "limit", value = "每页记录数", required = true)
                                 @PathVariable Long limit) {
        // 创建分页对象
        Page<EduTeacher> teacherPage = new Page<>(page, limit);

        // 调用方法进行分页，分页数据封装到teacherPage对象里面
        Map<String,Object> map = teacherService.getTeacherFrontList(teacherPage);
        return R.ok().data(map);
    }

    @ApiOperation("根据id获取一名讲师信息")
    @GetMapping("/getEntity/{id}")
    public R getEntity(@PathVariable("id") String id){
        // 1.获取讲师信息
        EduTeacher eduTeacher = teacherService.getById(id);

        // 2.获取讲师所讲课程信息
        LambdaQueryWrapper<EduCourse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EduCourse::getTeacherId,eduTeacher.getId());
        List<EduCourse> courseList = eduCourseService.list(queryWrapper);

        return R.ok().data("teacher", eduTeacher).data("courseList",courseList);
    }
}

